package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import beans.Account;
import beans.Comment;
import beans.Database;

public class CommentDAO {

	public static boolean insert(int threadId, int userId, String content) {
		Connection conn = null;
		PreparedStatement pre = null;

		try {
			conn = Database.connect();
			pre = conn
					.prepareStatement("INSERT INTO comment(thread_id,user_id,content) VALUES(?,?,?)");
			pre.setInt(1, threadId);
			pre.setInt(2, userId);
			pre.setString(3, content);

			int row = pre.executeUpdate();

			if (row > 0)
				return true;

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean deleteById(int id) {
		Connection conn = null;
		PreparedStatement pre = null;

		try {
			conn = Database.connect();
			pre = conn.prepareStatement("DELETE FROM comment WHERE id=?");
			pre.setInt(1, id);
			int row = pre.executeUpdate();
			if (row > 0)
				return true;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return false;
	}

	public static ArrayList<Comment> selectByThreadId(int threadId, int min,
			int max) {
		ArrayList<Comment> list = new ArrayList<>();
		try {
			Connection conn = Database.connect();
			PreparedStatement pre = conn
					.prepareStatement("SELECT comment.* FROM comment WHERE thread_id=? ORDER BY time ASC LIMIT ?,?");
			pre.setInt(1, threadId);
			pre.setInt(2, min);
			pre.setInt(3, max);
			ResultSet rs = pre.executeQuery();

			while (rs.next()) {
				Account acc = AccountDAO.selectSumaryById(rs.getInt("user_id"));
				Comment comment = new Comment(rs.getInt("id"), acc,
						rs.getString("content"), rs.getDate("time"));
				list.add(comment);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return list;
	}

	public static boolean update(int commentId, String content) {
		Connection conn = null;
		PreparedStatement pre = null;

		try {
			conn = Database.connect();
			pre = conn
					.prepareStatement("UPDATE comment SET content=? WHERE id=?");
			pre.setString(1, content);
			pre.setInt(2, commentId);

			int row = pre.executeUpdate();
			if (row > 0)
				return true;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * select to use edit
	 * 
	 * @param commentId
	 * @return
	 */
	public static Comment selectById(int commentId) {
		Comment comment = null;
		Connection conn = null;
		PreparedStatement pre = null;
		ResultSet rs = null;

		try {
			conn = Database.connect();
			pre = conn
					.prepareStatement("SELECT thread_id, content FROM comment WHERE id=?");
			pre.setInt(1, commentId);
			rs = pre.executeQuery();

			if (rs.next()) {
				comment = new Comment(commentId, rs.getString("content"));
				comment.setThreadId(rs.getInt("thread_id"));
			}

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return comment;
	}
}
